package com.dzsw.jedis.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


@ConfigurationProperties(prefix = "redis")
@Configuration
@Data
public class RedisConfig {
    private String host;
    private int port;
    private String password;
    private int timeout;
    private int maxTotal;
    private int maxIdle;
    private int numTestsPerEvictionRun;
    private int timeBetweenEvictionRunsMillis;
    private int minEvictableIdleTimeMillis;
    private int softMinEvictableIdleTimeMillis;
    private int maxWaitMillis;
    private boolean testOnBorrow;
    private boolean testWhileIdle;
    private boolean blockWhenExhausted;
    private boolean jmxEnabled;

    @Bean
    public JedisPool jedisPoolFactory() {
        System.out.println("JedisPool注入开始...");
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(jmxEnabled);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
        System.out.println("JedisPool注入成功...");
        return jedisPool;
    }

}
